# Create a descriptive plot of polarization and populist involvement by election.
# See Appendix H.




#############
# load data #
#############

library(brms)
library(ggplot2)
library(ggpubr)
library(stargazer)

load("PSRM Replication Files/PresidentialElectionsPosts.RData")

colnames(president_df)

length(unique(president_df$country)) # 26
length(unique(president_df$electionname)) # 29
length(unique(president_df$party_country)) # 52

# focus on 15 days before and after the election
day_range <- 15

sub <- president_df[which(president_df$daysinceelection >= -1*day_range
                          & president_df$daysinceelection <= day_range),]

# winners and losers
winners <- sub[which(sub$win == 1),]
losers <- sub[which(sub$win != 1),]

length(unique(winners$electionname[which(!is.na(winners$polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$economic_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$social_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_present))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_dummy))])) # 27

length(unique(losers$electionname[which(!is.na(losers$polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$economic_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$social_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_present))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_dummy))])) # 27




#####################
# polarization plot #
#####################

length(unique(winners$electionname[which(!is.na(winners$polarization))]))
length(unique(losers$electionname[which(!is.na(losers$polarization))]))

winner_elections <- unique(winners[which(!is.na(winners$polarization)),
                                   c("electionname", "polarization", "populist_present")])

loser_elections <- unique(losers[which(!is.na(losers$polarization)),
                                 c("electionname", "polarization", "populist_present")])

elections <- unique(rbind(loser_elections, winner_elections))

elections <- elections[order(elections$polarization, decreasing=FALSE),]

elections$electionname <- paste0(gsub("_", " (", elections$electionname), ")")

summary(elections$polarization)

elections$color <- "black"
elections$color[elections$populist_present == 1] <- "red"

# figure H.1
#pdf("polarizationrank.pdf", width=10, height=10)
par(mar=c(4.1,16,2.1,2.1))
plot(elections$polarization, 1:nrow(elections), type="n",
     ylab="", yaxt="n", xlab="Polarization",
     ylim=c(1.5,28.5))
abline(h=1:nrow(elections), col="gray95", lwd=0.5)
points(elections$polarization, 1:nrow(elections), pch=20, cex=2, col=elections$color)
axis(2, at=1:nrow(elections), labels=elections$electionname, las=1)
legend("bottomright", pch=20, col=c("red", "black"), 
       legend=c("Populist Involved", "No Populist"),
       bg="white")
#dev.off()
